// pages/my/info/info.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: app.globalData.userinfo,
    sexArr: ['女', '男']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userinfo: app.globalData.userinfo
    });
  },
  saveUserInfo: function () {

    var url = app.globalData.webUrl + 'Store/updatestore';
    var saveObject = {};
    saveObject.userid = this.data.userinfo.userid;
    saveObject.username = this.data.userinfo.username;
    saveObject.phone = this.data.userinfo.phone;
    saveObject.sex = this.data.userinfo.sex;
    saveObject.fullname = this.data.userinfo.fullname;
    saveObject.birthday = this.data.userinfo.birthday;

    app.loadDataFromServer(url, this.loadCallBack, saveObject);
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../../imgs/tips.png'
      });
      return;
    }

    wx.showToast({
      title: '修改成功~'
    });

    app.globalData.userinfo = this.data.userinfo;
  },
  changeDb: function (e) {
    var values = e.detail.value;
    var valuename = e.currentTarget.dataset.value;
    this.data.userinfo[valuename] = values;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bindPhone: function () {
    if (this.data.userinfo.phone){
      return;
    }
    wx.navigateTo({
      url: '../phone/bindphone',
    });
  },
  bindDateChange: function (e) {
    var val = e.detail.value;
    var userinfo = this.data.userinfo;
    userinfo.birthdaystr = val;
    var date = new Date(val + ' 00:00:00');
    userinfo.stamp = date.getTime();
    this.data.userinfo.birthday = userinfo.stamp/1000;
    console.log('bb=' + val + '=' + userinfo.stamp);
    this.setData({
      userinfo: userinfo
    });
  },
  bindSexChange:function(e){
    var val = e.detail.value;
    var userinfo = this.data.userinfo;
    console.log('val=' + val);
    userinfo.sex = val;
    userinfo.sexname = val==1 ? '男':'女';
    this.setData({
      userinfo: userinfo
    });
  }
})